﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;     // DLL support

namespace DTW_Monitor
{
    class NmeaParserDllInterface
    {
        #region Gets declaration
        [DllImport("NmeaParser.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern int GetLeftMotor();

        [DllImport("NmeaParser.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern int GetRightMotor();

        [DllImport("NmeaParser.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern float GetRearMotor();

        [DllImport("NmeaParser.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern float GetRearServo();
        #endregion //Gets declaration

        #region Sets declaration
        [DllImport("NmeaParser.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern int SetLeftMotor(int value);

        [DllImport("NmeaParser.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern int SetRightMotor(int value);

        [DllImport("NmeaParser.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern int SetRearMotor(float value);

        [DllImport("NmeaParser.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern int SetRearServo(float value);
        #endregion //Sets declaration

        [DllImport("NmeaParser.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern int GenerateDTWX3([MarshalAs(UnmanagedType.LPArray)] byte[] str);

        [DllImport("NmeaParser.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void Parse([MarshalAs(UnmanagedType.LPArray)] byte[] buffer, int bufferSize);

        #region Gets definition
        public static int GetLeftMotorDll()
        {
            return GetLeftMotor();
        }

        public static int GetRightMotorDll()
        {
            return GetRightMotor();
        }

        public static float GetRearMotorDll()
        {
            return GetRearMotor();
        }

        public static float GetRearServoDll()
        {
            return GetRearServo();
        }
        #endregion // Gets definition

        #region Sets definition
        public static int SetLeftMotorDll(int value)
        {
            return SetLeftMotor(value);
        }

        public static int SetRightMotorDll(int value)
        {
            return SetRightMotor(value);
        }

        public static int SetRearMotorDll(float value)
        {
            return SetRearMotor(value);
        }

        public static int SetRearServoDll(float value)
        {
            return SetRearServo(value);
        } 
        #endregion //Sets definition        
        
        public static string GenerateDTWX3Dll()
        {
            byte[] str = new byte[256];
            int outSize = GenerateDTWX3(str);
            return System.Text.Encoding.ASCII.GetString(str, 0, outSize);
        }

        public static void ParseDll(string data)
        {
            byte[] buffer = System.Text.Encoding.ASCII.GetBytes(data);
            Parse(buffer, buffer.Length);
        }
    }
}
